﻿#include "record.h"
#include "ui_record.h"
#include<QSqlDatabase>
#include<QListWidget>
#include<QListWidgetItem>
#include<QDebug>
#include"showrecord.h"
#include<QString>
#include<QPieSeries>
#include<QSqlQuery>
#include<QtCharts>
#include<QWidget>
#include<QChartView>
#include"maininterface.h"
record::record(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::record)
{
    ui->setupUi(this);
    num = 0;
    num1 = 0;
    //添加数据库驱动
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //设置数据库名
    db.setDatabaseName("G:\\Admin.db");
    //打开数据库
    if(!db.open())
    {
        qDebug()<<"open error";
    }
    time=QDateTime::currentDateTime().toString("MM-dd hh:mm");
    QListWidgetItem *Item= new QListWidgetItem();
    Showrecord *w=new Showrecord();
    //创建显示对象
    w->setattend(2,QString::fromLocal8Bit("学号"),QString::fromLocal8Bit("姓名"),QString::fromLocal8Bit("打卡状态"));
    //设置每一行item尺寸
    qDebug()<<w->sizeHint();
    Item->setSizeHint(w->sizeHint());
    ui->listWidget->addItem(Item);
    //把item的窗口设置为w
    ui->listWidget->setItemWidget(Item, w);


    //已打卡学生
    QString sql = QString("select Student.Number,Student.Name,Attenddence.Time from Student,Attenddence where Student.Number= Attenddence.Sno ");
    //.arg(time);
    QSqlQuery query(sql);
    qDebug()<<query.next();
    while(query.next())
    {
        QListWidgetItem *item = new QListWidgetItem();
        //创建显示对象
        Showrecord *w = new Showrecord();
        w->setattend(0,query.value(0).toString(),query.value(1).toString(),query.value(2).toString());
        qDebug()<<query.value(0).toString()<<query.value(1).toString()<<query.value(2).toString();
        //设置每一行item尺寸
        item->setSizeHint(w->sizeHint());
        ui->listWidget->addItem(item);
        //把item的窗口设置为w
        ui->listWidget->setItemWidget(item, w);
        qDebug()<<query.value(0).toInt()<<query.value(1).toString()<<query.value(2).toString();
        num++;
     }
    //未打卡学生

    //QString sql_1 =QString("select Stu.Sno,Stu.name from Stu where Stu.Sno not in (select Sno from Attend where Time like '%1')").arg(Time);
    QString sql_1 =QString("select Student.Number,Student.Name from Student where Student.Number not in (select Sno from Attenddence)");

    QSqlQuery query_1(sql_1);
    while(query_1.next())
    {
        QListWidgetItem *item = new QListWidgetItem();
        //创建显示对象
        Showrecord *w = new Showrecord();
        w->setattend(1,query_1.value(0).toString(),query_1.value(1).toString(),QString::fromLocal8Bit("未打卡"));
        //设置每一行item尺寸
        item->setSizeHint(w->sizeHint());
        ui->listWidget->addItem(item);
        //把item的窗口设置为w
        ui->listWidget->setItemWidget(item, w);
        num1++;
        qDebug()<<query_1.value(0).toInt()<<query_1.value(1).toString();
        //ui->textBrowser->append(str);

    }


    ui->lineEdit->setText(QString::number(num));
    ui->lineEdit_2->setText(QString::number(num1));

    int a = (num*1.0/(num+num1))*100;
    int b = (num1*1.0/(num+num1))*100;
    QString str = QString(QString::fromLocal8Bit("已打卡:%1%")).arg(a);
    QString str1 = QString(QString::fromLocal8Bit("未打卡:%1%")).arg(b);
    QPieSeries * series = new QPieSeries();
    // 构造两个饼状分区
    QPieSlice *slice_1 = new QPieSlice(str, a, this);
    slice_1->setLabelVisible(true); // 显示饼状区对应的数据label
    slice_1->setBrush(Qt::green);

    QPieSlice *slice_2 = new QPieSlice(str1, b, this);
    slice_2->setLabelVisible(true);
    slice_2->setBrush(Qt::blue);

    series->append(slice_1);
    series->append(slice_2);
    series->slices().at(0)->setColor(QColor(0,255,0));   //设置颜色
    series->slices().at(0)->setLabelColor(QColor(255,0,0));

    series->slices().at(1)->setColor(QColor(255,0,0));
    series->slices().at(1)->setLabelColor(QColor(255,0,0));

    QChart *chart = new QChart();
    chart->setTheme(QChart::ChartThemeLight);
    chart->legend()->hide();
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->setTitleBrush(QBrush(QColor(0, 0, 255)));//设置标题Brush
    chart->setTitleFont(QFont("微软雅黑"));//设置标题字体
    chart->setTitle(QString::fromLocal8Bit("当日考勤"));
    chart->setAnimationOptions(QChart::AllAnimations); // 设置显示时的动画效果
    ui->widget->setChart(chart);
}

record::~record()
{
    delete ui;
}
//返回上一级
void record::on_pB_back_clicked()
{
    MainInterface *w=new MainInterface();
    w->show();
    this->close();
}
